package com.thinkingstar.iads.cs.sys.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;
/**
 * 域,Shiro从Realm中获取安全数据(用户,角色,资源),可以吧Realm看做是安全数据源
 * @author Administrator
 *
 */
public class MyRealm implements Realm{

	/**
	 * 根据token获取认证信息
	 */
	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String username = (String) token.getPrincipal(); //用户名
		String password = new String((char[])token.getCredentials());
		//从数据源中核对用户名以及密码是否正确
		if(!"".equals(username)){
			//用户名错误
			throw new UnknownAccountException();
		}
		if(!"".equals(password)){
			//密码错误
			throw new IncorrectCredentialsException();
		}
		//验证成功,返回一个AuthenticationInfo实现
		return new SimpleAuthenticationInfo(username,password,getName());
	}

	/**
	 * 返回一个唯一的realm名字
	 */
	@Override
	public String getName() {
		return "myrealm";
	}

	/**
	 * 判断此realm是否支持此token(仅支持UsernamePasswordToken)
	 */
	@Override
	public boolean supports(AuthenticationToken token) {
		return token instanceof UsernamePasswordToken;
	}
	
	

}
